package day13.Ti.ZH;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Random;

public class Ti01 {
    public static void main(String[] args) {

        String[] box = {"电视机", "电冰箱", "电脑", "游戏机", "洗衣机"
                , "空调", "手机", "平板电脑", "电动车", "", "电饭煲"};
        List<String> list = new ArrayList<>(Arrays.asList(box));
        MyThread t1 = new MyThread(list);
        MyThread t2 = new MyThread(list);
        MyThread t3 = new MyThread(list);
        t1.setName("张三");
        t2.setName("李四");
        t3.setName("王五");
        t1.start();
        t2.start();
        t3.start();
    }
}

class MyThread extends Thread {
    private List<String> list;

    public MyThread(List<String> list) {
        this.list = list;
    }

    //定义List集合,存储自己抽到的奖品
    // (因为是成员变量,所以每个MyThread对象,都有一个自己的集合)
    private List<String> mylist = new ArrayList<>();

    @Override
    public void run() {
        while (true) {
            synchronized (MyThread.class) {
                int size = list.size();
                if (size > 0) {
//          随机一个数抽出来相当于奖品,然后把奖品从集合中删除,拿出来存到新集合中
                    int i = new Random().nextInt(size);
                    String s = list.remove(i);
                    mylist.add(s);
                } else {
                    System.out.println(getName() + ":" + mylist);
                    break;
                }
            }
        }
    }
}
